package LC;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName LC.LC15
 * @Description: TODO
 * @Author hl
 * @Date 2020/5/21
 * @Version V1.0
 **/
public class LC15 {
    public static void main(String[] args) {

    }
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> list=new ArrayList<>();
        if(nums==null||nums.length<3){
            return  null;
        }
        int len=nums.length;
        Arrays.sort(nums);
        for(int i=0;i<len-2;i++){
            if(nums[i]>0) break;
            if(nums[i]==nums[i+1]) continue;
            int L=i+1,R=len-1;
            while(L<R){
               int sum=nums[i]+nums[L]+nums[R];
               if(sum==0){
                   list.add(Arrays.asList(nums[i],nums[L],nums[R]));
                   while(nums[L]==nums[L+1]) L++;
                   while(nums[R]==nums[R-1]) R--;
                   L++;
                   R--;
               }else{
                   if(sum>0) R--;
                   else L++;
               }
            }

        }

      return  list;
    }
}
